#include "LedMatrix.h"

/**
 * @brief  led闪烁线程
 * @note   
 * @param  *argument: 
 * @retval None
 */
void LedTask(void *argument)
{
    RTC_DateTypeDef sdatestructure;
    RTC_TimeTypeDef stimestructure;

    for (;;)
    {
        for (int x = 0; x < 8; x++)
        {
            for (int y = 0; y < 8; y++)
            {
                lightOneDot(y, x);
                osDelay(500);
//                printf("LED X->%d,Y->%d\n", x, y);
                /* Get the RTC current Time ,must get time first*/
                HAL_RTC_GetTime(&hrtc, &stimestructure, RTC_FORMAT_BIN);
                /* Get the RTC current Date */
                HAL_RTC_GetDate(&hrtc, &sdatestructure, RTC_FORMAT_BIN);
                /* Display date Format : yy/mm/dd */
//                printf("%02d/%02d/%02d\r\n", 2000 + sdatestructure.Year, sdatestructure.Month, sdatestructure.Date);
                /* Display time Format : hh:mm:ss */
//                printf("%02d:%02d:%02d\r\n", stimestructure.Hours, stimestructure.Minutes, stimestructure.Seconds);
            }
        }
    }
}
/**
 * @brief  发送字节
 * @note   
 * @param  byte: 
 * @retval None
 */
void HC595_Send_Byte(uint8_t byte)
{
    uint8_t i;
    uint8_t shit_Data = byte;
    for (i = 0; i < 8; i++)
    {
        if (shit_Data & 0x80)
            HC595_LED_DATA_HIGH();
        else
            HC595_LED_DATA_LOW();
        HC595_SHIFT_LOW();
        __nop();
        __nop();
        HC595_SHIFT_HIGH();
        shit_Data <<= 1;
    }
}
/**
 * @brief  锁存
 * @note   
 * @retval None
 */
void HC595_CS(void)
{
    HC595_STOR_LOW();
    __nop();
    __nop();
    HC595_STOR_HIGH();
}
/**
 * @brief  发送数组
 * @note   
 * @param  *array: 
 * @param  array_len: 
 * @retval None
 */
void HC595_Send_Array(uint8_t *array, uint16_t array_len)
{
    //    HC595_OE_Enable();
    __nop();
    __nop();
    for (int i = 0; i < array_len; i++)
    {
        HC595_Send_Byte(array[i]);
    }
    HC595_CS();
    //    __nop();
    //    __nop();
    //    HC595_OE_Disable();
}
/**
 * @brief  第一个字节是列选，反电平
	       第二个字节是行选，正常电平
 * @note   
 * @param  col: 列号，0-7
 * @param  row: 行号，0-7
 * @retval None
 */
void lightOneDot(int col, int row)
{

    uint8_t col_row[2] = {0};

    col_row[0] = ~(1 << col);
    col_row[1] = 1 << row;

    HC595_Send_Array(col_row, sizeof(col_row));
}
